﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using EFERP;
using HRM.DICTIONARY.UI;
using HRM.DICTIONARY.DAL;
using HRM.DICTIONARY.LOGINUI;
using HRM.DICTIONARY.DATABASEUI;
using DevExpress.XtraEditors;
using DevExpress.Utils;

namespace HRM
{
    public partial class frmMainHRM : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private xfrmxucAll frmXucAll;
        private xfrmSYSUSER frmUser;
        private xfrmPermission frmPermission;
        private xfrmEmployee frmEmployee;
        private xfrmWorkingProcess frmWorkingProcess;
        private xfrmWork frmWork;
        private xfrmContract frmcontract;
        private xfrmAllOption frmAllOption;

        private WaitDialogForm dlg = null;public void SetWaitDialogCaption(string fCaption)
        {
            if (this.dlg != null)
            {
                this.dlg.Caption = fCaption;
            }
            else
            {
                CreateWaitDialog(fCaption);
                if (this.dlg != null)
                {
                    this.dlg.Caption = fCaption;
                }
            }
        }
        public void CreateWaitDialog(string fCaption)
        {
            this.dlg = new WaitDialogForm(fCaption, "Vui lòng đợi trong giấy lát...");
        }
        protected virtual void DoHide()
        {
            //base.OnLoad(EventArgs.Empty);
            if (this.dlg != null)
            {
                this.dlg.FormClosing += new FormClosingEventHandler(this.dlg_FormClosing);
                this.dlg.Close();
            }
        }
        private void dlg_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.dlg.Dispose();
            this.dlg = null;
        }

        ERP_Entities ERPEn = new ERP_Entities();
        public frmMainHRM()
        {
            InitializeComponent();
            StaticVariable.ERPEn = ERPEn;
            CheckLogin();
        }

        private void CheckLogin()
        {
            xfrmLogin frmLogin = new xfrmLogin();
            frmLogin.ShowDialog(this);
            if (StaticVariable.UserLogin != null)
            {
                //Access Right
            }
            else
            {
                if (frmLogin.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    Environment.Exit(0);
                }
            }
        }
        private void CloseAllForm()
        {
            if (this.mdiTab.Pages.Count > 0)
                this.mdiTab.Pages.Clear();
            //new SendEmailRerporter
            //SysLog.Insert("");
        }
        private void CallForm(string Name)
        {
            switch (Name)
            {
                case "frmxucAll":
                    SetWaitDialogCaption("Đang tải danh mục hệ thống...");
                    if (this.frmXucAll != null)
                    {
                        this.mdiTab.Pages[this.frmXucAll].MdiChild.Activate();
                    }
                    else
                    {
                        this.Cursor = Cursors.WaitCursor;
                        xfrmxucAll frmDic = new xfrmxucAll
                        {
                            MdiParent = this
                        };
                        this.frmXucAll = frmDic;
                        this.frmXucAll.Text = "Quản lý danh mục";
                        this.frmXucAll.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                        this.frmXucAll.Show();
                        this.Cursor = Cursors.Default;
                    }
                    break;

                case "xfrmSYSUSER":
                    SetWaitDialogCaption("Đang tải danh mục người dùng...");
                    if (this.frmUser != null)
                    {
                        this.mdiTab.Pages[this.frmUser].MdiChild.Activate();
                    }
                    else
                    {
                        this.Cursor = Cursors.WaitCursor;
                        xfrmSYSUSER frmUser = new xfrmSYSUSER
                        {
                            MdiParent = this
                        };
                        this.frmUser = frmUser;
                        this.frmUser.Text = "Quản lý người dùng";
                        this.frmUser.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                        this.frmUser.Show();
                        this.Cursor = Cursors.Default;
                    }
                    break;

                case "xfrmPermission":
                    SetWaitDialogCaption("Đang tải danh mục phân quyền hệ thống...");
                    if (this.frmUser != null)
                    {
                        this.mdiTab.Pages[this.frmUser].MdiChild.Activate();
                    }
                    else
                    {
                        this.Cursor = Cursors.WaitCursor;
                        xfrmPermission frmPermission = new xfrmPermission
                        {
                            MdiParent = this
                        };
                        this.frmPermission = frmPermission;
                        this.frmPermission.Text = "Phân quyền người dùng";
                        this.frmPermission.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                        this.frmPermission.Show();
                        this.Cursor = Cursors.Default;
                    }
                    break;

                case "xfrmEmployee":
                    SetWaitDialogCaption("Đang tải danh mục nhân viên...");
                    if (this.frmEmployee != null)
                    {
                        this.mdiTab.Pages[this.frmEmployee].MdiChild.Activate();
                    }
                    else
                    {
                        this.Cursor = Cursors.WaitCursor;
                        xfrmEmployee frmEmployee = new xfrmEmployee
                        {
                            MdiParent = this
                        };
                        this.frmEmployee = frmEmployee;
                        this.frmEmployee.Text = "Lý lịch theo nhân viên";
                        this.frmEmployee.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                        this.frmEmployee.Show();
                        this.Cursor = Cursors.Default;
                    }
                    break;

                case "xfrmWorkingProcess":
                    SetWaitDialogCaption("Đang tải quá trình làm việc...");
                    if (this.frmWorkingProcess != null)
                    {
                        this.mdiTab.Pages[this.frmWorkingProcess].MdiChild.Activate();
                    }
                    else
                    {
                        this.Cursor = Cursors.WaitCursor;
                        xfrmWorkingProcess frmWorkingProcess = new xfrmWorkingProcess
                        {
                            MdiParent = this
                        };
                        this.frmWorkingProcess = frmWorkingProcess;
                        this.frmWorkingProcess.Text = "Quá trình làm việc theo nhân viên";
                        this.frmWorkingProcess.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                        this.frmWorkingProcess.Show();
                        this.Cursor = Cursors.Default;
                    }
                    break;

                case "xfrmWork":
                    SetWaitDialogCaption("Đang tải danh mục quản lý công việc...");
                    if (this.frmWork != null)
                    {
                        this.mdiTab.Pages[this.frmWork].MdiChild.Activate();
                    }
                    else
                    {
                        this.Cursor = Cursors.WaitCursor;
                        xfrmWork frmWork = new xfrmWork
                        {
                            MdiParent = this
                        };
                        this.frmWork = frmWork;
                        this.frmWork.Text = "Quá trình làm việc theo nhân viên";
                        this.frmWork.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                        this.frmWork.Show();
                        this.Cursor = Cursors.Default;
                    }
                    break;

                case "xfrmContract":
                    SetWaitDialogCaption("Đang tải danh mục quản lý hợp đồng...");
                    if (this.frmcontract != null)
                    {
                        this.mdiTab.Pages[this.frmcontract].MdiChild.Activate();
                    }
                    else
                    {
                        this.Cursor = Cursors.WaitCursor;
                        xfrmContract xfrmContract = new xfrmContract
                        {
                            MdiParent = this
                        };
                        this.frmcontract = xfrmContract;
                        this.frmcontract.Text = @"Hợp đồng nhân viên";
                        this.frmcontract.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                        this.frmcontract.Show();
                        this.Cursor = Cursors.Default;
                    }
                    break;

                case "xfrmAllOption":
                    //SetWaitDialogCaption("Đang tải tùy chọn...");
                    //if (this.frmAllOption != null)
                    //{
                    //    this.mdiTab.Pages[this.frmAllOption].MdiChild.Activate();
                    //}
                    //else
                    //{
                    //    this.Cursor = Cursors.WaitCursor;
                    //    xfrmAllOption xfrmAllOption = new xfrmAllOption
                    //    {
                    //        MdiParent = this
                    //    };
                    //    this.frmAllOption = xfrmAllOption;
                    //    this.frmAllOption.Text = @"Tùy Chọn";
                    //    this.frmAllOption.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                    //    this.frmAllOption.Show();
                    //    this.Cursor = Cursors.Default;
                    //}
                    break;
            }
            DoHide();
        }

        #region Call form into MDI Tab
        private void btnDanhSachNhanVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
        private void btnDanhMuc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("frmxucAll");
        }
        private void btnNguoiDung_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("xfrmSYSUSER");
        }
        private void btnPhanQuyen_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("xfrmPermission");
        }
        private void btnDSNVTheoNV_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("xfrmEmployee");
        }
        private void btnQuaTrinhLamViec_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
        private void btnQTLVTheoNV_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("xfrmWorkingProcess");
        }
        private void btnQTLVTheoDanhSach_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("xfrmWorkingProcessList");
        }
        private void btnQuanLyCongViec_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("xfrmWork");
        }

        private void btnHopDongLaoDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CallForm("xfrmContract");
        }

        private void btnTuyChon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new xfrmAllOption().ShowDialog();
            //CallForm("xfrmAllOption");
        }
        #endregion Call form into MDI Tab

        void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            XtraForm frm = (XtraForm)sender;
            switch (frm.Name)
            {
                case "frmxucAll":
                    this.frmXucAll = null;
                    break;
                case "xfrmSYSUSER":
                    this.frmUser = null;
                    break;
                case "xfrmPermission":
                    this.frmPermission = null;
                    break;
                case "xfrmEmployee":
                    this.frmEmployee = null;
                    break;
                case "xfrmWorkingProcess":
                    this.frmPermission = null;
                    break;
                case "xfrmWorkingProcessList":
                    this.frmPermission = null;
                    break;
                case "xfrmWork":
                    this.frmWork = null;
                    break;

                case "xfrmContract":
                    this.frmcontract = null;
                    break;

                case "xfrmAllOption":
                    this.frmAllOption = null;
                    break;
            }
            
        }
        private void btnKetThuc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CloseAllForm();
            //Environment.Exit(-1);
            Application.Exit();
        }
        private void btnDangXuat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //SYS_LOG.Insert("Hệ Thống", "Đăng xuất");
            this.btnDangXuat.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            xfrmLogin classic = new xfrmLogin();
            classic.ShowDialog(this);
            this.btnDangXuat.Enabled = true;
        }
        private void btnDoiMatKhau_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new xfrmChangePassword().ShowDialog();
        }
        private void btnNhatKy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
        private void btnSaoLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new xfrmBackupDatabase().ShowDialog();
        }
        private void btnPhucHoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new xfrmRestoreDatabase().ShowDialog();
        }

       

       
    }
}
